package crystal.workshop.ui.renderer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import mujl.ui.DefaultSelectionModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MenuBar;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;

import crystal.renderer.RendererPanel;
import crystal.workshop.ui.StructureDataModel;
import crystal.workshop.ui.StructuresListTableUI;

public class CrystalPanel extends JPanel {
	
	class EventHandler implements ChangeListener {
		
		// ListSelectionListener interface ////////////////////////////////////
		@Override
	    public void stateChanged(final ChangeEvent e) {
	        // If cell selection is enabled, both row and column change events are fired
	        if (e.getSource() == CrystalPanel.this.getStructureSelectionModel()) {
	        	// Tell the list that the selection has changed and needs to be
	        	// updated
	        	CrystalPanel.this.structuresChanged(e);
	        }
	    }		
	};

	private RendererPanel jRendererPane = null;
	
	private EventHandler eventHandler = new EventHandler();
	
	protected DefaultSelectionModel<Collection<StructureDataModel>> strSelectionModel;	

	public DefaultSelectionModel<Collection<StructureDataModel>> getStructureSelectionModel() {
		return strSelectionModel;
	}
	
	public void setStructureSelectionModel(
			final DefaultSelectionModel<Collection<StructureDataModel>> newValue)
	{
		if(strSelectionModel != newValue) {
			//final DefaultSelectionModel<Collection<StructureDataModel>> oldValue =
			//	getStructureSelectionModel();
			if(strSelectionModel != null) {
				strSelectionModel.removeChangeListener(eventHandler);
			}
			strSelectionModel = newValue;
			
			// Listen for events from the new object
			if(strSelectionModel != null) {
				strSelectionModel.addChangeListener(eventHandler);
			}
		}
	}
	
	/**
	 * This method initializes 
	 * 
	 */
	public CrystalPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setLayout(new BorderLayout());
        this.add(getRendererPane(), BorderLayout.CENTER);
	}
	
	private RendererPanel getRendererPane() {
		if (jRendererPane == null) {
			jRendererPane = new RendererPanel();
		}
		return jRendererPane;
	}
	
	private void structuresChanged(final ChangeEvent e) {
		StructureDataModel str = strSelectionModel.getSelection().iterator().next();
		if(str != null) {
			jRendererPane.setCrystal(str.getCrystal());
		}
	}

	public static void main(String[] args) {
		final JFrame frame = new JFrame();
		
		final JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		panel.add(new JButton("TEST"), BorderLayout.WEST);
		panel.add(new CrystalPanel(), BorderLayout.CENTER);
		
		frame.setContentPane(panel);
		
		frame.setMenuBar(new MenuBar());
		
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.dispose();
                System.exit(0);
            }
        });
        
        frame.setSize(300, 300);
        frame.setVisible(true);
	      
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
